package spider.impl.novel;

import org.jsoup.select.Elements;
import spider.NovelSiteEnum;
import spider.entitys.Novel;
import spider.util.NovelSpiderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 看书中网站的书籍列表爬取
 * created by yb
 * on 2017/8/14.
 **/
public class KanShuZhongNovelSpider extends AbstractNovleSpider {
    /**
     * @param url 某个网站的所有小说列表所在的url
     * @return 所有的小说具体信息
     */
    @Override
    public List< Novel > getsNovel( String url , Integer maxTryTimes) {
        List< Novel > novels = new ArrayList<>();
        try {
            Elements trs = super.getsTr( url ,maxTryTimes);
            for ( int i = 1, size = trs.size()-1; i < size; i++ ) {
                Novel novel = new Novel();
                Elements tds = trs.get( i ).getElementsByTag( "td" );
                novel.setType( tds.get( 0 ).text() );
                novel.setName( tds.get( 1 ).text() );
                novel.setUrl( tds.get( 1 ).getElementsByTag( "a" ).first().absUrl( "href" ) );
                novel.setLastUpdateChapter( tds.get( 2 ).text() );
                novel.setLastUpdateChapterUrl( tds.get( 2 ).getElementsByTag( "a" ).first().absUrl( "href" ) );
                novel.setAuthor( tds.get( 3 ).text() );
                novel.setLastUpdateTime( NovelSpiderUtil.getDate( tds.get( 4 ).text(), "MM-dd" ) );
                novel.setStatus( NovelSpiderUtil.getNovelStatus( tds.get( 5 ).text() ) );
                novel.setPlatformId( NovelSiteEnum.getNovelSiteEnumByUrl( url ).getId() );
                novels.add( novel );
            }
        } catch ( Exception e ) {
            throw new RuntimeException( e );
        }
        return novels;
    }

}
